# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 3
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

import wx
import os
import vp

class wxvpFrame(wx.Frame):
    """Main frame for wxVP."""
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, title="wxVP v. 0.1", size=(1024,768))
        self.CreateStatusBar()
        
        # set up about dialog info
        about = wx.AboutDialogInfo()
        #about.AddDeveloper("Christopher Koch")
        about.SetCopyright("(C) Copyright 2013 Christopher Koch")
        about.SetName("wxVP")
        about.SetDescription("wxPython-based Volition Package viewer and editor")
        about.SetVersion("0.1 alpha")
        about.SetWebSite("http://code.google.com/p/wxvp/")
        self.about = about

        # set up menu bar
        filemenu = wx.Menu()
        file_new = filemenu.Append(wx.ID_NEW, "New VP",
            "Open an empty VP in a new tab")
        file_open = filemenu.Append(wx.ID_OPEN, "Open VP...",
            "Open an existing VP in a new tab")
        filemenu.AppendSeparator()
        file_save = filemenu.Append(wx.ID_SAVE, "Save VP",
            "Save the current VP")
        file_saveas = filemenu.Append(wx.ID_SAVEAS, "Save VP As...",
            "Save the current VP as a new file")
        filemenu.AppendSeparator()
        file_close = filemenu.Append(wx.ID_CLOSE, "Close",
            "Close the selected tab")
        filemenu.AppendSeparator()
        file_exit = filemenu.Append(wx.ID_EXIT, "&Exit", "")
        
        editmenu = wx.Menu()
        edit_addfile = editmenu.Append(wx.ID_ANY, "Add file(s)...",
            "Import file(s) into the VP")
        edit_adddir = editmenu.Append(wx.ID_ANY, "Add directory...",
            "Import a directory into the VP")
        editmenu.AppendSeparator()
        edit_removefile = editmenu.Append(wx.ID_ANY, "Remove file(s)",
            "Remove selected file(s) from the VP")
        edit_removedir = editmenu.Append(wx.ID_ANY, "Remove directory",
            "Remove selected directory from the VP")
        editmenu.AppendSeparator()
        edit_extrfile = editmenu.Append(wx.ID_ANY, "Extract file(s)...",
            "Extract selected file(s) from the VP")
        edit_extrdir = editmenu.Append(wx.ID_ANY, "Extract directory...",
            "Extract selected directory from the VP")
        
        helpmenu = wx.Menu()
        help_about = helpmenu.Append(wx.ID_ABOUT, "&About", "")

        menubar = wx.MenuBar()
        menubar.Append(filemenu, "&File")
        menubar.Append(editmenu, "&Edit")
        menubar.Append(helpmenu, "&Help")

        self.SetMenuBar(menubar)
        
        self.Bind(wx.EVT_MENU, self.OnAbout, help_about)
        self.Bind(wx.EVT_MENU, self.OnExit, file_exit)
        
        self.Bind(wx.EVT_MENU, self.OnNew, file_new)
        self.Bind(wx.EVT_MENU, self.OnOpen, file_open)
        self.Bind(wx.EVT_MENU, self.OnClose, file_close)
        
        # set up notebook
        self.nb = wx.Notebook(self)
        self.nb.AddPage(wxvpTab(self.nb), "*New file")
        
        self.Show(True)
        
    def OnAbout(self, e):
        wx.AboutBox(self.about)
        
    def OnExit(self, e):
        self.Close(True)
        
    def OnNew(self, e):
        self.nb.AddPage(wxvpTab(self.nb), "*New file")
        
    def OnOpen(self, e):
        dirname = ''
        dlg = wx.FileDialog(self, "Choose a file", dirname, "", "*.vp", wx.OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            filename = dlg.GetFilename()
            dirname = dlg.GetDirectory()
            f = open(os.path.join(dirname, filename), 'rb')
            self.SetStatusText("Opening {}...".format(filename))
            self.vp_file = vp.VolitionPackageFile(f)
            f.close()
            this_tab = self.nb.GetCurrentPage()
            if this_tab.changed:
                # open a warning, do you want to save or open in new tab?
                pass
            tab_num = self.nb.GetSelection()
            self.nb.SetPageText(tab_num, filename)
            # operate on this_tab.dir_view and this_tab.file_view here
            self.SetStatusText("Done!")
        dlg.Destroy()
        
    def OnClose(self, e):
        # tab close, not app close
        cur_tab = self.nb.GetSelection()
        self.nb.DeletePage(cur_tab)
        
        
class wxvpTab(wx.Panel):
    def __init__(self, parent, filepath=None):
        wx.Panel.__init__(self, parent)
        
        self.filepath = filepath
        self.changed = False
        
        main_sizer = wx.BoxSizer(wx.HORIZONTAL)
        dir_sizer = wx.BoxSizer(wx.VERTICAL)
        file_sizer = wx.BoxSizer(wx.VERTICAL)
        
        splitter = wx.SplitterWindow(self)
        splitter.SetSashGravity(0.4)
        
        dir_panel = wx.Panel(splitter)
        file_panel = wx.Panel(splitter)
        
        self.dir_view = wx.TreeCtrl(dir_panel)
        self.file_view = wx.ListView(file_panel)
        
        splitter.SplitVertically(dir_panel, file_panel)
        main_sizer.Add(splitter, proportion=1, flag=wx.EXPAND)
        dir_sizer.Add(self.dir_view, proportion=1, flag=wx.EXPAND)
        file_sizer.Add(self.file_view, proportion=1, flag=wx.EXPAND)
        dir_panel.SetSizerAndFit(dir_sizer)
        file_panel.SetSizerAndFit(file_sizer)
        self.SetSizerAndFit(main_sizer)


def display_gui():
    """Called from wxvp.py if the -nogui flag is not used."""
    app = wx.App(False)
    frame = wxvpFrame(None)
    app.MainLoop()

if __name__ == "__main__":
    display_gui()